home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 201-225 / disk_223 / csh / run.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  218 lines

  1.  
  2. /*
  3.  * RUN.C
  4.  *
  5.  * (c)1986 Matthew Dillon     9 October 1986
  6.  *
  7.  *    RUN   handles running of external commands.
  8.  *
  9.  * Version 2.07M by Steve Drew 10-Sep-87
  10.  *
  11.  * Version 3.03A by Carlo Borreo & Cesare Dieni 12-May-89
  12.  *
  13.  */
  14.  
  15. extern char *v_path;
  16. char *FindIt();
  17.  
  18. do_run(str)
  19. char *str;
  20. {
  21. int i, len, retcode;
  22. char buf[200]; /* enough space for 100 char cmd name + path stuff */
  23. char *path;
  24.  
  25. char *p = av[0];
  26. char **args = av+1;
  27.  
  28. while(*p++) *p &= 0x7F;      /* allow "com mand" */
  29.  
  30. while(*args) {                /* if any arg contains a space then */
  31.     if (index(*args,' ')) {        /* surround with quotes, since must */
  32.     i = strlen(*args);        /* of specified via "arg u ment" on */
  33.     movmem(*args,(*args)+1,i);    /* original command line.        */
  34.     args[0][0] = args[0][i+1] = '\"';    /* mpush in execom.c has    */
  35.     args[0][i+2] = '\0';        /* allowed for these 2 extra bytes. */
  36.     }
  37.     ++args;
  38.     }
  39. if ((len = strlen(av[0])) > 100) { ierror(NULL,509); return -1; }
  40. if (path = FindIt(av[0],"",buf)) retcode = myfexecv(path, av);
  41. else {
  42.     Myprocess->pr_WindowPtr = (APTR)(-1);
  43.     /*
  44.      * manx's fexecv code only allows us 38
  45.      * chars for command name.
  46.      */
  47.     if (len > 37) av[0][37] = '\0';
  48.     retcode = myfexecv(av[0], av);
  49.     Myprocess->pr_WindowPtr = NULL;
  50.     }
  51. if (retcode < 0) {
  52.     char *copy;
  53.     if ((path = FindIt(av[0],".sh",buf)) == NULL) {
  54.         fprintf(stderr,"Command Not Found %s\n",av[0]);
  55.         return -1;
  56.         }
  57.     av[1] = buf;               /* particular to do_source() */
  58.     copy = malloc(strlen(str)+3);
  59.     sprintf(copy,"x %s",str);
  60.     retcode = do_source(copy);
  61.     free(copy);
  62.     }
  63. return retcode;
  64. }
  65.  
  66. char *dofind(cmd, ext, buf)
  67. char *cmd, *ext, *buf;
  68. {
  69. char *ptr, *s;
  70.  
  71. sprintf(buf,"%s%s",cmd,ext);
  72. if (exists(buf)) return buf;
  73. if (BaseName(buf)==buf) {
  74.     s = get_var(LEVEL_SET, v_path);
  75.     while (*s) {
  76.         for (ptr=buf; *s && *s!=','; ) *ptr++ = *s++;
  77.         sprintf(ptr, "%s%s", cmd, ext);
  78.         if (exists(buf)) return buf;
  79.         if (*s) s++;
  80.         }
  81.     }
  82. return NULL;
  83. }
  84.  
  85. char *FindIt(cmd,ext,buf)
  86. char *cmd, *ext, *buf;
  87. {
  88. char *response;
  89.  
  90. Myprocess->pr_WindowPtr = (APTR)(-1);
  91. response=dofind(cmd,ext,buf);
  92. Myprocess->pr_WindowPtr = NULL;
  93. return response;
  94. }
  95.  
  96. myfexecv(cmd, argv)
  97. char *cmd, **argv;
  98. {
  99. long ret_val;
  100. struct FileHandle *fhp;
  101. APTR sav_ret;
  102. register char **ap, *cp, *arg;
  103. long len, seg, sav, stksiz;
  104. char buf[40];
  105. union {
  106.     long *lp;
  107.     long ll;
  108.     } l, stk;
  109. long oldcin, oldcout;
  110. long doexec();
  111. extern long _savsp;
  112.  
  113. if (seg = LoadPrg(cmd)) goto found;
  114. l.lp = (long *) Mycli->cli_CommandDir;
  115. while (l.ll) {
  116.     l.ll <<= 2;
  117.     sav = CurrentDir(l.lp[1]);
  118.     seg = LoadPrg(cmd);
  119.     CurrentDir(sav);
  120.     if (seg) goto found;
  121.     l.ll = *l.lp;
  122.     }
  123. sprintf(buf, "c:%s", cmd);
  124. if (seg = LoadPrg(buf)) goto found;
  125. return -1;
  126.  
  127. found:
  128.  
  129. stksiz = 4 * Mycli->cli_DefaultStack;
  130. if ((stk.lp = AllocMem(stksiz+8, 0L)) == 0) {
  131.     UnLoadPrg(seg);
  132.     return -1;
  133.     }
  134. for (len=0,ap=argv+1;*ap;ap++)
  135.     len += strlen(*ap) + 1;
  136. if (len==0) len++;
  137. if ((cp = arg = AllocMem(len, 0L)) == 0) {
  138.     UnLoadPrg(seg);
  139.     FreeMem(stk.lp, stksiz+8);
  140.     return -1;
  141.     }
  142. *stk.lp = stksiz + 8;
  143. stk.ll += stksiz;
  144. stk.lp[0] = stksiz;
  145. stk.lp[1] = ((long *)_savsp)[2];
  146. sav_ret = Myprocess->pr_ReturnAddr;
  147. Myprocess->pr_ReturnAddr = (APTR) stk.lp;
  148.  
  149. sav = Mycli->cli_Module;
  150. Mycli->cli_Module = seg;
  151.  
  152. for (ap=argv+1;*ap;ap++) {
  153.     strcpy(cp, *ap);
  154.     if (ap[1]) strcat(cp, " ");
  155.     cp += strlen(cp);
  156.     }
  157. if (len==1) arg[1]='\0';
  158. arg[len-1] = '\n';
  159.  
  160. cp = (char *)((long)Mycli->cli_CommandName << 2);
  161. movmem(cp, buf, 40);
  162. strcpy(cp+1, cmd);
  163. cp[0] = strlen(cmd);
  164.  
  165. fhp = (struct FileHandle *) (Myprocess->pr_CIS << 2);
  166. strncpy(fhp->fh_Buf<<2, arg, (int)(len < 200?len:199));
  167. fhp->fh_Pos = 0;
  168. fhp->fh_End = len < 200?len:199;
  169. oldcin  = Myprocess->pr_CIS;
  170. oldcout = Myprocess->pr_COS;
  171.  
  172. ret_val = doexec(len, stksiz, stksiz+8, len, arg, (seg+1)<<2, stk.ll);
  173.  
  174. Myprocess->pr_CIS = oldcin;
  175. Myprocess->pr_COS = oldcout;
  176. fhp->fh_Pos = fhp->fh_End;
  177. UnLoadPrg(Mycli->cli_Module);
  178. Myprocess->pr_ReturnAddr = sav_ret;
  179. Mycli->cli_Module = sav;
  180. FreeMem(arg, len);
  181. movmem(buf, cp, 40);
  182. return ret_val;
  183. }
  184.  
  185. static long doexec()
  186. {
  187. #asm
  188.     movem.l    d3-d7/a2-a5,-(sp)        ;save registers
  189.     lea        savsp(pc),a0
  190.     move.l    sp,(a0)                    ;save our sp
  191.     movem.l    8(a5),d0/d2/d3/d4/a0/a4/a7    ;load params
  192.     move.l    4(sp),a3                ;get old sp from CLI
  193.     movem.l    4(a3),a1/a2/a5/a6        ;get BCPL environment
  194.     move.l    d0,12(a1)                ;set length
  195.     move.l    a0,d1                    ;copy to dreg
  196.     lsr.l    #2,d1                    ;convert to BPTR
  197.     move.l    d1,8(a1)                ;set ptr
  198.     move.l    a0,d1                    ;copy to d1 as well
  199.     jsr        (a4)                    ;call new program
  200.     movem.l    (sp)+,d2/d3                ;get stk siz and old sp
  201.     move.l    sp,a1                    ;save current sp
  202.     move.l    savsp(pc),sp            ;get back our sp
  203.     movem.l    (sp)+,d3-d7/a2-a5        ;get back registers
  204.     move.l    d0,-(sp)                ;save return code
  205.     sub.l    d2,a1                    ;back up a bit
  206.     sub.l    #8,a1                    ;back up over header
  207.     move.l    (a1),d0                    ;get size to free
  208.     move.l    4,a6                    ;get ExecBase
  209.     jsr        -210(a6)                ;free the memory
  210.     move.l    (sp)+,d0                ;get the return code
  211. #endasm
  212. }
  213.  
  214. #asm
  215. savsp:
  216.     dc.l    0
  217. #endasm
  218.